# This file is part of The Vu Toolkit.
#
# The Vu Toolkit is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3.0 of the License, or (at your option) any later version.
#
# The Vu Toolkit is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with The Vu Toolkit; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# 02111-1307 USA.
#
# Copyright © Zak Fenton 2008
#

require 'Vu/Frame'
require 'Vu/cairo_help'
require 'Vu/Label'

module Vu
	# A clickable button
	class Button < Frame
		def initialize (str = nil)
			super()
			resize_border!(10,10)
			if str
				self.content = Label.new(str)
			end
		end
		
		def resize! (w,h)
			super(w,h)
		end
		
		@focus = false
		
		def has_focus
			@focus
		end
		
		def render (cr)
			cr.line_width = 2
			cr.rounded_rectangle(1,1,width-2,height-2, 8)
			
			if @focus
				cr.set_source_rgb(0.85, 0.875, 0.85)
			else
				cr.set_source_rgba(0.85,0.85,0.85,0.95)
			end
			cr.fill_preserve
			
			cr.set_source_rgb(0.75,0.75,0.75)
			
			cr.set_source_rgb(0,0,0) if @focus
			cr.set_source_rgb(1,0,0) if @uberfocus
			cr.stroke
			
			# Don't forget the children
			super(cr)
		end
		
		def mouse_enter
			@focus = true
			request_render_all
		end
		
		def mouse_press (b, x, y)
			if b == 1
				@uberfocus = true
				request_render_all
			end
		end
		
		def mouse_release (b,x,y)
			uf = @uberfocus
			@uberfocus = false
			if uf
				request_render_all
			end
		end
		
		def mouse_exit
			@focus = @uberfocus = false
			request_render_all
		end
		
		# anyone got a better name for the left button being down?
		@uberfocus = false
		
		def is_down
			@uberfocus
		end
		
		@action_handler = false
		
		def mouse_click (b,x,y)
			if b == 1 and @action_handler
				@action_handler.call()
			end
		end
		
		def set_action (&h)
			@action_handler = h
		end
	end
end

